package log

import (
	"github.com/sirupsen/logrus"
	"os"
)

const (
	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel = iota
	// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel
)

var (
	// RootLoggerName - Root logger name, contains all log data
	rootLoggerName = "root"
	// RootLogger - Root Logger
	rootLogger = myLogger()
)

// NamedLogger - Returns a logger wrapped with pkg/stream fields
func NamedLogger(pkg string) *logrus.Entry {
	return rootLogger.WithFields(logrus.Fields{
		"pkg": pkg,
	})
}

func myLogger() *logrus.Logger {
	my := logrus.New()
	my.Formatter = &logrus.TextFormatter{}
	my.Out = os.Stdout
	my.SetLevel(DebugLevel)
	my.SetReportCaller(true)
	return my
}

func SetLevel(level int) {
	rootLogger.SetLevel(levelFrom(level))
}

// levelFrom - returns level from int
func levelFrom(level int) logrus.Level {
	switch level {
	case 0:
		return PanicLevel
	case 1:
		return FatalLevel
	case 2:
		return ErrorLevel
	case 3:
		return WarnLevel
	case 4:
		return InfoLevel
	case 5:
		return DebugLevel
	case 6:
		return TraceLevel
	}
	return DebugLevel
}
